package com.bestvike.stone.spring.rabbit;

import com.bestvike.stone.core.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * Created by 许崇雷 on 2017-11-01.
 */
@Component
@ConditionalOnClass(RabbitTemplate.class)
public final class RabbitUtils {
    private static RabbitTemplate rabbitTemplate;

    RabbitUtils(RabbitTemplate rabbitTemplateInstance) {
        rabbitTemplate = rabbitTemplateInstance;
    }

    public static void convertAndSend(RabbitInfo rabbitInfo, Object object) {
        Assert.notNull(rabbitInfo, "rabbitInfo can not be null");
        Assert.notNull(object, "object can not be null");

        rabbitTemplate.convertAndSend(rabbitInfo.getExchange(), rabbitInfo.getRoutingKey(), object);
    }

    public static void retry(String queue, RabbitRetryMessage message) {
        Assert.notNull(queue, "queue can not be null");
        Assert.notNull(message, "message can not be null");

        rabbitTemplate.convertAndSend(StringUtils.EMPTY, queue, message);
    }
}
